package com.travel.server.manager.impl.command.customer.data;

import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.server.manager.dto.request.EmptyRequest;
import com.travel.server.manager.dto.response.customer.data.QueryCustomerDistributionResponse;
import com.travel.server.manager.dto.response.customer.data.QueryCustomerDistributionResponse.DistributionItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.CustomerDal;
import com.travel.server.manager.model.dto.data.CustomerAreaDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/10 16:25
 * @Desc:
 */
@Component
@Slf4j
public class QueryCustomerDistributionCommand extends AbstractCommand<EmptyRequest, QueryCustomerDistributionResponse> {

    @Autowired
    private CustomerDal customerDal;

    @Override
    protected void checkInput(EmptyRequest request) {

    }

    @Override
    public QueryCustomerDistributionResponse handle(EmptyRequest request) {
        QueryCustomerDistributionResponse response = new QueryCustomerDistributionResponse();
        List<DistributionItem> distributionItemList = new ArrayList<>();
        List<CustomerAreaDto> list = customerDal.selectDistribution();;
        if(!CollectionUtil.isEmpty(list)){
            for(CustomerAreaDto dto : list){
                DistributionItem item = new DistributionItem();
                item.setArea(dto.getLiveProvince());
                item.setNum(dto.getCount());
                distributionItemList.add(item);
            }
            for(String area : CommonUtil.AREA_LIST){
                boolean add = true;
                DistributionItem item = new DistributionItem();
                for(CustomerAreaDto dto : list){
                    if(area.equals(dto.getLiveProvince())){
                        add = false;break;
                    }
                }
                if(add){
                    item.setArea(area);
                    item.setNum(0);
                    distributionItemList.add(item);
                }
            }
        }
        response.setDistributionList(distributionItemList);
        return response;
    }
}
